package com.dongdongsongcan.app.model.api;

import com.alibaba.fastjson.JSON;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AreaData;
import com.dongdongsongcan.app.control.bean.AuthnumData;
import com.dongdongsongcan.app.control.bean.CategoryData;
import com.dongdongsongcan.app.control.bean.DDLocationData;
import com.dongdongsongcan.app.control.bean.DeliverPriceData;
import com.dongdongsongcan.app.control.bean.LabelData;
import com.dongdongsongcan.app.control.bean.MenuGroupData;
import com.dongdongsongcan.app.control.bean.MenuListData;
import com.dongdongsongcan.app.control.bean.OrderDetailData;
import com.dongdongsongcan.app.control.bean.OrderListData;
import com.dongdongsongcan.app.control.bean.OrderStatusData;
import com.dongdongsongcan.app.control.bean.RecommendAdData;
import com.dongdongsongcan.app.control.bean.ShopData;
import com.dongdongsongcan.app.control.bean.UserData;
import com.dongdongsongcan.app.control.bean.UserFavorableData;
import com.su.dolphin.control.bean.BaseBean;
import com.su.dolphin.core.proxy.HttpProxy;
import com.su.dolphin.utils.LogUtil;

import java.util.HashMap;

public class WebServiceApi {

    public static CategoryData getCategoryData() {
        return getData(CategoryData.class, ApiUrl.CATEGROY, null);
    }

    public static RecommendAdData getRecommendAdData() {
        return getData(RecommendAdData.class, ApiUrl.AD, null);
    }

    public static LabelData getLabelData() {
        return getData(LabelData.class, ApiUrl.LABEL, null);
    }


    public static ShopData getShops(String shop_label, String shop_type, String orderby, int offset, int pagesize, double site_x, double site_y) {
        // per_page=11&limit=1&shop_type=&orderby=
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("site_x", site_x + "");
        params.put("site_y", site_y + "");
        params.put("shop_label", shop_label);
        params.put("shop_type", shop_type + "");
        params.put("orderby", orderby + "");
        params.put("per_page", offset + "");
        params.put("limit", pagesize + "");
        return getData(ShopData.class, ApiUrl.SHOP, params);
    }

    public static ShopData getShopInfo() {
        return getData(ShopData.class, ApiUrl.SHOPINFO, null);
    }

    public static MenuGroupData getMenuGroupData(int shopid) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("shopid", shopid + "");
        return getData(MenuGroupData.class, ApiUrl.MENUGROUP, params);
    }

    // shopid=189&groupid=241&per_page=&limit=
    public static MenuListData getMenuListData(int shopid, int groupid,
                                               int per_page, int limits) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("shopid", shopid + "");
        params.put("groupid", groupid + "");
        return getData(MenuListData.class, ApiUrl.MENULIST, params);
    }

    public static AreaData getAreas(int area_pid) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("area_pid", area_pid + "");
        return getData(AreaData.class, ApiUrl.ADDRESS, params);
    }

    public static AreaData search(String keyword) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("keyword", keyword);
        return getData(AreaData.class, ApiUrl.SEARCH, params);
    }


    public static ShopData article(int article_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("article_id", article_id + "");
        return getData(ShopData.class, ApiUrl.ARTICLE, params);
    }

    public static AuthnumData authnum(String user_mobile) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_mobile", user_mobile + "");
        return getData(AuthnumData.class, ApiUrl.AUTHNUM, params);
    }

    // user_mobile=1397445214&authnum=6557&session_id=i0644i70g6ami4057f1eg677r1
    public static UserData register_mobile(String user_mobile, String authnum,
                                           String session_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_mobile", user_mobile + "");
        params.put("authnum", authnum + "");
        params.put("session_id", session_id + "");
        return getData(UserData.class, ApiUrl.REGISTER_MOBILE, params);
    }


    // user_mobile=1397445214&authnum=6557&session_id=i0644i70g6ami4057f1eg677r1
    public static UserData mobile_login(String user_mobile, String authnum,
                                        String session_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_mobile", user_mobile + "");
        params.put("authnum", authnum + "");
        params.put("session_id", session_id + "");
        return getData(UserData.class, ApiUrl.MOBILE_LOGIN, params);
    }

    // user_name=admin&user_pwd=admin
    public static UserData user_login(String username, String user_pwd) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", username);
        params.put("user_pwd", user_pwd);
        return getData(UserData.class, ApiUrl.USER_LOGIN, params);
    }

    // token=e00cf25ad42683b3df678c61f42c6bda&user_name=admin
    public static AddressData user_address_list() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("token", getToken());
        return getData(AddressData.class, ApiUrl.USER_ADDRESS_LIST, params);
    }

    // token=e00cf25ad42683b3df678c61f42c6bda&user_name=admin
    // today month previous
    public static OrderListData order(String date, int offset, int pagesize) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("date", date);
        params.put("token", getToken());
        params.put("user_name", getUserName());
        params.put("per_page", offset + "");
        params.put("limit", pagesize + "");
        return getData(OrderListData.class, ApiUrl.ORDER, params);
    }

    // token=e00cf25ad42683b3df678c61f42c6bda&user_name=admin&order_id=100
    public static OrderDetailData orderInfo(String order_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("token", getToken());
        params.put("user_name", getUserName());
        params.put("order_id", order_id);
        return getData(OrderDetailData.class, ApiUrl.ORDER_INFO, params);
    }

    //token=874bae5fd6af4ec4f69ada5f0be63b2e&user_name=15666220075
    public static UserFavorableData user_favorable(int shopId) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("shopId", shopId + "");
        params.put("token", getToken());
        params.put("user_name", getUserName());
        return getData(UserFavorableData.class, ApiUrl.USER_FAVORABLE, params);
    }

    //    "token": getToken(),
    //            "user_name": "admin",
    //            "order_louhao1": "\u697c\u53f7",
    //            "order_name": "\u59d3\u540d",
    //            "order_sex": "\u7537",
    //            "order_mobile": "135865478547",
    //            "order_arrive": "\u5c3d\u5feb\u9001\u5230",
    //            "order_beta": "\u5907\u6ce8",
    //            "order_shop_id": "19812",
    //            "order_pay_method": "afterpayment",
    //            "order_area_id": "163",
    // order_foods string
    public static OrderDetailData order_insert(String orderSubmitData) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("order", orderSubmitData);
        return postData(OrderDetailData.class, ApiUrl.ORDER_INSERT, params);
    }

    //&info_id=1000&info[info_area_id]=189
    public static BaseBean user_address_update(AddressData.Address address) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("token", getToken());
        params.put("info[info_louhao1]", address.getInfo_louhao1());
        params.put("info[info_area_id]", address.getInfo_area_id());
        params.put("info[info_mobile]", address.getInfo_mobile());
        params.put("info[info_name]", address.getInfo_name());
        params.put("info[info_sex]", address.getInfo_sex());
        return postData(BaseBean.class, ApiUrl.USER_ADDRESS_UPDATE, params);
    }

    public static BaseBean user_address_del(int info_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("info_id", info_id + "");
        params.put("token", getToken());
        return getData(BaseBean.class, ApiUrl.USER_ADDRESS_DEL, params);
    }

    //http://kjcms.jcy24.com/openapi/index.php/user/user_address_insert?token=af2087cea4e4199e8d5db80cda8f5d3e
    // &user_name=15666220075&info[info_louhao1]
    // =1155&info[info_area_id]=156&info[info_mobile]=13987458745&info[info_name]=test&info[info_sex]=%E5%B0%8F%E5%A7%90
    public static BaseBean user_address_insert(AddressData.Address address) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("token", getToken());
        params.put("user_name", getUserName());
        params.put("info[info_louhao1]", address.getInfo_louhao1());
        params.put("info[info_area_id]", address.getInfo_area());
        params.put("info[info_mobile]", address.getInfo_mobile());
        params.put("info[info_name]", address.getInfo_name());
        params.put("info[info_sex]", address.getInfo_sex());
        return postData(BaseBean.class, ApiUrl.USER_ADDRESS_INSERT, params);
    }

    public static DDLocationData address_nearby(double latitude, double longitude) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("site_x", String.valueOf(longitude));
        params.put("site_y", String.valueOf(latitude));
        return postData(DDLocationData.class, ApiUrl.USER_ADDRESS_NEARBY, params);
    }

    //area_id=50&shop_id=55
    public static DeliverPriceData distance_price(String area_id, int shop_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("area_id", area_id);
        params.put("shop_id", shop_id + "");
        return postData(DeliverPriceData.class, ApiUrl.DISTANCE_PRICE, params);
    }

    //token=874bae5fd6af4ec4f69ada5f0be63b2e&user_name=15666220075&order_code=177151012163154849
    public static OrderStatusData order_status(String order_code) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("token", getToken());
        params.put("order_code", order_code + "");
        return postData(OrderStatusData.class, ApiUrl.ORDER_STATUS, params);
    }

    //token=874bae5fd6af4ec4f69ada5f0be63b2e&user_name=15666220075&order_code=177151012163154849
    public static ShopData user_collect() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("token", getToken());
        return getData(ShopData.class, ApiUrl.USER_COLLECT, params);
    }

    //token=874bae5fd6af4ec4f69ada5f0be63b2e&user_name=15666220075&order_code=177151012163154849
    public static ShopData user_collect_insert(int shop_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("token", getToken());
        params.put("shop_id", shop_id + "");
        return getData(ShopData.class, ApiUrl.USER_COLLECT_INSERT, params);
    }
    //token=874bae5fd6af4ec4f69ada5f0be63b2e&user_name=15666220075&order_code=177151012163154849
    public static ShopData user_collect_del(int shop_id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("user_name", getUserName());
        params.put("token", getToken());
        params.put("shop_id", shop_id + "");
        return getData(ShopData.class, ApiUrl.USER_COLLECT_DEL, params);
    }
    //token=874bae5fd6af4ec4f69ada5f0be63b2e&user_name=15666220075&order_code=177151012163154849
    public static ShopData searchshop(String keyword) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("keyword", keyword);
        return getData(ShopData.class, ApiUrl.SEARCHSHOP, params);
    }


    private static <T> T postData(Class<T> clazz, String url,
                                  HashMap<String, String> params) {
        String resultString = HttpProxy.i().post(url, params);
        T result = JSON.parseObject(resultString, clazz);
        if (result != null) {
            LogUtil.d("object result: " + result.toString());
        } else {
            BaseBean baseBean = new BaseBean();
            baseBean.setError(BaseBean.STATUS_EXCEPTION);
            LogUtil.d("Json parse error ");
        }
        return result;
    }

    private static <T> T getData(Class<T> clazz, String url,
                                 HashMap<String, String> params) {
        String resultString = HttpProxy.i().get(url, params);
        LogUtil.d(resultString);
        T result = JSON.parseObject(resultString, clazz);
        if (result != null) {
            LogUtil.d("object result: " + result.toString());
        } else {
            BaseBean baseBean = new BaseBean();
            baseBean.setError(BaseBean.STATUS_EXCEPTION);
            LogUtil.d("Json parse error ");
        }
        return result;
    }

    private static String getToken() {
        if (UserControl.i().userExist()) {
            return UserControl.i().getUser().getToken();

        } else {
            return null;
        }
    }

    private static String getUserName() {
        if (UserControl.i().userExist()) {
            return UserControl.i().getUser().getUser_name();

        } else {
            return null;
        }
    }
}
